// functionPage/pages/othersHomePage/othersHomePage.js
import { concernUser } from '../../../utils/tools';
import { giveLikeApi, submitComment } from '../../../utils/tools.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        uid: '',
        // 是否是自己本身
        myself: false,
        scrollMove: false,
        ipxBottom: false,
        isIphone: false,
        //气泡高度
        backHeight: '',
        backTop: '',
        //个人信息
        realname: '',
        realpicurl: '',
        phone: '',
        cityTeam: '',
        // 打卡信息
        checkincitynum: 0,
        checkincountrynum: 0,
        checkinnum: 0,
        enterNum: 0,
        experienceNum: 0,
        // 切换nav
        current: 0,
        //日记本
        noInfoDiary: false,
        // 日记内容
        experienceArr: [],
        pageNumDiary: 0,
        loadingDiary: true,
        loadingAllDiary: false,
        //日记操作弹框
        othersBtnShow: false,
        operateIndex: Number,
        // 手机号操作相关
        show: false,
        actions: [
            {
                name: '复制',
                color: '#333333',
                value: 'copy',
            },
            {
                name: '',
                color: '#0091ff',
                value: 'call',
            },
        ],
        idDiary: '',
        //照片墙
        // 照片墙--缺省显示
        noInfo: false,
        // 照片墙--上传照片
        sceneImageArr: [],
        // 照片墙--图片描述
        description: [],
        // 照片墙--发布人
        userArr: [],
        addressArr: [],
        // 照片墙--发布时间
        timeArr: [],
        idArr: [],
        userIdArr: [],
        pageNum: 0,
        loading: true,
        loadingAll: false,
        // 当前描述索引
        currentPicIndex: 0,
        //关注按钮
        concernflg: '00',
        remarkValue: '', //评论
        remarkBox: '',
        remarkId: '',
        remarkEdit: false,
        anonymous: false, //评论匿名
    },
    //我的录入页面
    submitTap(e) {
        // wx.showToast({
        //     title: '敬请期待',
        //     icon: 'none',
        //     duration: 2000,
        // });
        // return;
        wx.navigateTo({
            url: `../minePage/mySubmitInfo/mySubmitInfo?enterNum=${this.data.enterNum}&uid=${this.data.uid}`,
        });
    },
    experienceTap(e) {
        wx.navigateTo({
            url: `../minePage/myExperience/myExperience?uid=${this.data.uid}`,
        });
    },
    photoWallTap(e) {
        wx.navigateTo({
            url: `../../../pages/photoWall/photoWall?title=${this.data.realname}·图片墙&personalflg=00&fromWhere=02`,
        });
    },
    // 打卡数页面
    checkTap(e) {
        wx.navigateTo({
            url: `../../../pages/minePage/checkNumber/checkNumber?checkinnum=${this.data.checkinnum}&uid=${this.data.uid}`,
        });
    },
    countryTap(e) {
        wx.navigateTo({
            url: `../../../pages/minePage/countryNumber/countryNumber?checkincountrynum=${this.data.checkincountrynum}&uid=${this.data.uid}`,
        });
    },
    cityTap(e) {
        wx.navigateTo({
            url: `../../../pages/minePage/cityNumber/cityNumber?checkincitynum=${this.data.checkincitynum}&uid=${this.data.uid}`,
        });
    },
    //写日志
    editTap() {
        wx.navigateTo({
            url: `../../releasePage/pages/addDailyRecord/addDailyRecord`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _that = this;
        _that.setData({
            cityTeam: app.globalData.localcityTeam,
            uid: options.uid,
            myself: app.globalData.uid === options.uid,
        });
        if (app.globalData.isIpx) {
            _that.setData({
                ipxBottom: true,
            });
        }
        wx.getSystemInfo({
            success: function (res) {
                let isIpx;
                //model中包含着设备信息
                var model = res.model;
                if (model.search('iPhone') != -1) {
                    isIpx = true;
                } else {
                    isIpx = false;
                }
                _that.setData({
                    isIphone: isIpx,
                });
            },
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //tabbar
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 3,
            });
        }
        // 获取导航栏气泡高度
        let bubbleInfo = wx.getMenuButtonBoundingClientRect();
        this.setData({
            backHeight: bubbleInfo.height,
            backTop: bubbleInfo.top,
        });
        this.getminePageNum();
        this.getDiaryList();
        this.getInfoList();
    },
    //写完日记加载
    editBackload() {
        this.setData({
            experienceArr: [],
            pageNumDiary: 0,
            loadingDiary: true,
            loadingAllDiary: false,
        });
        this.getDiaryList();
    },
    //切换nav
    changeNav(e) {
        this.setData({
            current: e.currentTarget.dataset.index,
        });
    },
    swiperChange(e) {
        this.setData({
            current: e.detail.current,
        });
    },
    // 日记本操作
    //显示弹出层
    operateBtn(e) {
        this.setData({
            othersBtnShow: true,
            operateIndex: e.currentTarget.dataset.index,
        });
    },
    //显示弹出层遮罩层
    operateHideBtn(e) {
        this.setData({
            othersBtnShow: false,
        });
    },
    //点赞
    supportBtn(e) {
        let _that = this;
        let flg = e.target.dataset.praiseflg == '00' ? '01' : '00';
        _that.data.experienceArr[
            e.target.dataset.index
        ].describe.praiseflg = flg;
        _that.setData({
            othersBtnShow: false,
            experienceArr: _that.data.experienceArr,
        });
        giveLikeApi('13', e.target.dataset.id, flg);
        wx.showToast({
            title: flg == '01' ? '点赞成功' : '已取消点赞',
            icon: 'none', //如果要纯文本，不要icon，将值设为'none'
            duration: 1000,
        });
    },
    // 隐藏匿名
    removeFocus() {
        this.setData({
            remarkEdit: false,
            remarkBox: false,
        });
    },
    // 切换匿名
    toggle() {
        this.setData({
            anonymous: !this.data.anonymous,
        });
    },
    remarkBtn(e) {
        this.setData({
            othersBtnShow: false,
            remarkEdit: true,
            remarkBox: true,
            remarkId: e.target.dataset.id,
        });
    },
    //评论
    remarkInput(e) {
        this.setData({
            remarkValue: e.detail.value,
        });
    },
    remarkBlur() {
        this.setData({
            remarkBox: false,
            remarkEdit: false,
        });
    },
    //提交评论
    submitComment() {
        let _that = this;
        if (!_that.data.remarkValue) return;
        submitComment(
            '',
            _that.data.remarkId,
            '13',
            '00',
            _that.data.remarkValue,
            _that.data.anonymous ? '01' : '00'
        ).then((res) => {
            if (res.status == 0) {
                _that.setData({
                    remarkValue: '',
                });
            }
        });
    },
    // 图片预览
    imgPreviewTap(e) {
        this.setData({
            currentPicIndex: e.currentTarget.dataset.current,
        });
        let fromWhere = '02';
        if (this.data.uid == app.globalData.uid) {
            fromWhere = '00';
        }
        wx.navigateTo({
            url: `../../../pages/checkIn/imgPreview/imgPreview?fromWhere=${fromWhere}`,
        });
    },
    //打电话
    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: e, //仅为示例，并非真实的电话号码
        });
    },
    //关注按钮
    concernBtn(e) {
        let _that = this;
        _that.setData(
            {
                concernflg: _that.data.concernflg == '00' ? '01' : '00',
            },
            () => {
                concernUser(_that.data.uid, _that.data.concernflg, function () {
                    wx.showToast({
                        title:
                            _that.data.concernflg == '00'
                                ? '已取消关注'
                                : '已关注',
                        icon: 'none',
                        duration: 2000,
                    });
                    //从关注列表进入关注后刷新
                    let pages = getCurrentPages(); //当前页面栈id
                    let prevPage = pages[pages.length - 2]; //上一页面
                    let prevPage2 = pages[pages.length - 3];
                    let prevPage3 = pages[pages.length - 4];
                    if (typeof prevPage.getMyConcernList == 'function') {
                        prevPage.setData({
                            inputValue: '',
                        });
                        prevPage.getMyConcernList();
                    }
                    if (
                        typeof prevPage2.getConcernUserDiaryList == 'function'
                    ) {
                        prevPage2.setData(
                            {
                                diaryArr: [],
                                pageNum: 0,
                            },
                            () => {
                                prevPage2.getConcernUserDiaryList();
                                prevPage2.getminePageNum();
                            }
                        );
                    }
                    if (typeof prevPage3.getMyConcernList == 'function') {
                        prevPage3.getminePageNum();
                    }
                });
            }
        );
    },
    //滚动显示关注列表、顶部
    //顶部显示姓名
    scrolltoupper(e) {
        console.log(e.detail.scrollTop);
        if (e.detail.scrollTop > 270) {
            this.setData({
                scrollMove: true,
            });
        } else {
            this.setData({
                scrollMove: false,
            });
        }
    },
    //获取个人信息
    getminePageNum(e) {
        let _that = this;
        wx.request({
            url: app.globalData.rootPath + `cjMap/personalPageNum`,
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
                authId: app.globalData.authId,
                uid: _that.data.uid,
            },
            success: function (res) {
                console.log(res.data);
                _that.setData({
                    checkincitynum: res.data.data.checkincitynum,
                    checkincountrynum: res.data.data.checkincountrynum,
                    checkinnum: res.data.data.checkinnum,
                    realname: res.data.personalInfo.realname,
                    realpicurl: res.data.personalInfo.realnapicurl,
                    phone: res.data.personalInfo.phone,
                    cityTeam: res.data.personalInfo.localcity || '湛腾',
                    concernflg: res.data.personalInfo.concernflg,
                    enterNum: res.data.data.enternum,
                    experienceNum: res.data.data.experiencenum,
                });
            },
        });
    },
    //获取日记列表
    getDiaryList(e) {
        let _that = this;
        if (_that.data.pageNumDiary == 0 || _that.data.current == 1) {
            if (!_that.data.loadingAllDiary) {
                _that.setData({
                    pageNumDiary: _that.data.pageNumDiary + 1,
                    loadingDiary: true,
                    loadingAllDiary: false,
                });
                wx.request({
                    url: app.globalData.rootPath + `cjMap/getDiaryList`,
                    method: 'GET',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded',
                    },
                    data: {
                        authId: app.globalData.authId,
                        createuid: _that.data.uid, //如果不填就是返本人的
                        pageNo: _that.data.pageNumDiary,
                        pageSize: 10,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            for (let i = 0; i < res.data.data.length; i++) {
                                let descValue = {
                                    id: '',
                                    time: '2019-12-12 13:28',
                                    describeText: '',
                                    createuid: '',
                                    praiseflg: '',
                                };
                                let arrValue = {
                                    describe: {},
                                    imageArr: [],
                                };
                                descValue.id = res.data.data[i].id;
                                descValue.praiseflg =
                                    res.data.data[i].praiseflg;
                                descValue.time =
                                    res.data.data[i].createtime.slice(0, 4) +
                                    '-' +
                                    res.data.data[i].createtime.slice(5, 7) +
                                    '-' +
                                    res.data.data[i].createtime.slice(8, 10) +
                                    ' ' +
                                    res.data.data[i].createtime.slice(11, 16);
                                descValue.describeText =
                                    res.data.data[i].content;
                                descValue.createuid =
                                    res.data.data[i].createuid;
                                arrValue.describe = descValue;
                                arrValue.imageArr = res.data.data[i].picurls;
                                _that.data.experienceArr.push(arrValue);
                            }
                            _that.setData({
                                experienceArr: _that.data.experienceArr,
                                loadingDiary: false,
                            });
                            if (res.data.data.length < 10) {
                                _that.setData({
                                    loadingAllDiary: true,
                                });
                            }
                            if (!_that.data.experienceArr.length) {
                                _that.setData({
                                    noInfoDiary: true,
                                    loadingDiary: false,
                                    loadingAllDiary: false,
                                });
                            } else {
                                _that.setData({
                                    noInfoDiary: false,
                                });
                            }
                        } else {
                            if (res.data.errmsg == '输入页码超出范围') {
                                _that.setData({
                                    loadingAllDiary: true,
                                    loadingDiary: false,
                                });
                            }
                        }
                    },
                });
            }
        }
    },
    //获取照片墙
    getInfoList(e) {
        let _that = this;
        if (_that.data.pageNum == 0 || _that.data.current == 0) {
            if (!_that.data.loadingAll) {
                _that.setData({
                    pageNum: _that.data.pageNum + 1,
                    loading: true,
                    loadingAll: false,
                });
                wx.request({
                    url: app.globalData.rootPath + `cjMap/getPhotoWallList`,
                    method: 'GET',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded',
                    },
                    data: {
                        authId: app.globalData.authId,
                        uid: _that.data.uid,
                        pageSize: 10,
                        pageNo: _that.data.pageNum,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            for (let i = 0; i < res.data.data.length; i++) {
                                _that.data.sceneImageArr.push(
                                    res.data.data[i].picurl
                                );
                                _that.data.description.push(
                                    res.data.data[i].picdescription
                                );
                                _that.data.userArr.push(
                                    res.data.data[i].createuname
                                );
                                _that.data.addressArr.push(
                                    res.data.data[i].cityname
                                );
                                let checkinTime = app.timeago(
                                    res.data.data[i].checkintime ||
                                        res.data.data[i].createtime
                                );
                                _that.data.timeArr.push(checkinTime);
                                _that.data.idArr.push(res.data.data[i].id);
                                _that.data.userIdArr.push(
                                    res.data.data[i].createuid
                                );
                            }
                            _that.setData({
                                sceneImageArr: _that.data.sceneImageArr,
                                description: _that.data.description,
                                userArr: _that.data.userArr,
                                timeArr: _that.data.timeArr,
                                idArr: _that.data.idArr,
                                userIdArr: _that.data.userIdArr,
                                addressArr: _that.data.addressArr,
                                loading: false,
                            });
                            if (res.data.data.length < 10) {
                                _that.setData({
                                    loadingAll: true,
                                });
                            }
                            if (!_that.data.sceneImageArr.length) {
                                _that.setData({
                                    noInfo: true,
                                    loading: false,
                                    loadingAll: false,
                                });
                            } else {
                                _that.setData({
                                    noInfo: false,
                                });
                            }
                        } else {
                            if (res.data.errmsg == '输入页码超出范围') {
                                _that.setData({
                                    loadingAll: true,
                                    loading: false,
                                });
                            }
                        }
                    },
                });
            }
        }
    },
    // 编辑
    edit(e) {
        let { phone } = e.currentTarget.dataset;
        let actions = [
            {
                name: '复制',
                color: '#333333',
                value: 'copy',
                phone: phone,
            },
            {
                name: phone,
                color: '#0091ff',
                value: 'call',
                phone: phone,
            },
        ];

        this.setData({
            show: true,
            actions,
        });

        // 导航栏置灰
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#484848',
            animation: {
                duration: 300,
                timingFunc: 'easeIn',
            },
        });
    },
    // 取消编辑
    onCancel() {
        // 导航栏置白
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#ffffff',
            animation: {
                duration: 300,
                timingFunc: 'easeIn',
            },
        });
        this.setData({
            show: false,
        });
    },
    // 选中后
    onSelect(e) {
        console.log(e);
        let _that = this;
        if (e.detail.value == 'copy') {
            _that.onCancel();
            // 再次编辑都是默认type 01
            wx.setClipboardData({
                data: e.detail.phone,
                success: function (res) {
                    wx.getClipboardData({
                        success: function (res) {},
                    });
                },
            });
        } else if (e.detail.value == 'call') {
            _that.onCancel();
            _that.callPhone(e.detail.phone);
        }
    },
});
